package com.imkit.hitutor

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.util.Log
import com.imkit.hitutor.ui.chatInBanking.ChatInBankingActivity
import com.imkit.hitutor.ui.chatInBanking.ChatInBankingListActivity
import com.imkit.hitutor.ui.networkingChat.NetworkingChatActivity
import com.imkit.hitutor.ui.networkingChat.NetworkingChatListActivity
import com.imkit.sdk.ApiResponse
import com.imkit.sdk.IMKit
import com.imkit.sdk.IMRestCallback
import com.imkit.sdk.model.Client
import com.imkit.sdk.model.Room
import com.imkit.widget.utils.Utils
import retrofit2.Call

class DemoApplication : Application() {

    companion object {
        lateinit var application: DemoApplication;
        lateinit var packName: String;

        fun configureIM(url: String?, key: String?) {
            IMKit.instance()
                .setUrl(url)
                .setClientKey(key)
                .setBucketName("imkit")
                .setProviderAuthority("${packName}.fileProvider")
                .setSSLTrustAll(true)
                .init(application)
            Log.d("TAG", "onCreate: DemoApplication")
            IMKit.setDebugLog(true)
        }

        fun connect(context: Context, clientId: String, accessToken: String, callback: FlutterCallback) {
            IMKit.instance().connect(clientId, accessToken, object : IMRestCallback<Client?>() {
                override fun onResult(result: Client?) {
                    DemoUtils.setLogin(context, true)
                    DemoUtils.setClientId(context, clientId)
                    callback.onResult(true, "")
                }

                override fun onFailure(call: Call<ApiResponse<Client?>>, t: Throwable) {
                    DemoUtils.setLogin(context, false)
                    callback.onResult(false, t.message)
                }
            })
        }

        fun updateUserInfo(context: Context, nickName: String, avatarUrl: String, callback: FlutterCallback) {
            IMKit.instance().updateCurrentUserInfo(nickName, avatarUrl, object : IMRestCallback<Client?>() {
                override fun onResult(result: Client?) {
                    DemoUtils.setClientName(context, nickName)
                    callback.onResult(true, "")
                }

                override fun onFailure(p0: Call<ApiResponse<Client?>>, p1: Throwable) {
                    callback.onResult(false, p1.message)
                }
            })
        }

        fun openChatPage(context: Activity, inviteId: String, callback: FlutterCallback) {
            IMKit.instance().createRoomWithUser(inviteId, object : IMRestCallback<Room?>() {
                override fun onResult(result: Room?) {
                    context.startActivity(
                        Intent(context, NetworkingChatActivity::class.java)
                            .putExtra(NetworkingChatActivity.ROOM_ID, result?.id)
                            .putExtra(NetworkingChatActivity.ROOM_TITLE, Utils.getDisplayRoomTitle(context, result))
                    )
                    callback.onResult(true, "")
                }

                override fun onFailure(p0: Call<ApiResponse<Room?>>, p1: Throwable) {
                    super.onFailure(p0, p1)
                    callback.onResult(false, p1.message)
                }
            })
        }

        fun openRoomList(context: Context) {
            context.startActivity(Intent(context, NetworkingChatListActivity::class.java))
        }
    }

    private val SAMPLE_ENCRYPTION_KEY = "sample-encryption-key"

    override fun onCreate() {
        super.onCreate()
        application = this;
        packName = packageName
    }

}